<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      class="tab-control"
      @tabClick="tabClick"
      ref="tabControl1"
      v-show="isTabFixed"
    ></tab-control>
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
      :pull-up-load="true"
      @pullingUp="loadMore"
    >
      <home-swiper
        :banners="banners"
        @swiperImageLoad="swiperImageLoad"
      ></home-swiper>
      <recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      ></tab-control>
      <goodslist :goods="showGoods" />
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
//导入导航栏模块（从组件common中）
import NavBar from "components/common/navbar/NavBar.vue";
import TabControl from "components/content/tabcontrol/TabControl.vue";
import Goodslist from "components/content/goods/GoodsList.vue";
import Scroll from "components/common/scroll/Scroll.vue";

//导入首页所需的模块
import HomeSwiper from "./childComps/HomeSwiper"; //轮播图
import RecommendView from "./childComps/RecommendView"; //推荐模块
import FeatureView from "./childComps/FeatureView"; //本周流行模块

//网络请求
import { getHomeMultidata, getHomeGoods } from "network/home";
import { itemListenerMixin, backTopMixin } from "common/mixin";

export default {
  name: "Home",
  components: {
    NavBar,

    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    Goodslist,
    Scroll,
  },
  mixins: [itemListenerMixin, backTopMixin],
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      isShowBackTop: false,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  created() {
    //1.请求多个数据
    this.getHomeMultidata(),
      //2.请求商品信息
      this.getHomeGoods("pop"),
      this.getHomeGoods("new"),
      this.getHomeGoods("sell");
  },
  //进入组件生命周期函数
  activated() {
    //进入组件设置其显示位置
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
  },
  //离开组件生命周期函数
  deactivated() {
    //1.保存离开组件时的位置
    this.saveY = this.$refs.scroll.getScrollY();

    //2.取消全局事件的监听
    this.$bus.$off("itemImageLoad", this.itemImgListener);
  },
  mounted() {},
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  methods: {
    /*
事件监听相关的方法 
 */
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    //返回顶部事件监听
    //返回顶部的显示与隐藏
    contentScroll(position) {
      //1.判断BackTop是否显示
      this.isShowBackTop = -position.y > 1000;
      //2.决定tabcontrol是否吸顶
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },
    //上拉加载事件
    loadMore() {
      this.getHomeGoods(this.currentType);
    },
    //获取新品tabcontrol导航栏距离顶部的值
    swiperImageLoad() {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },

    /*
  网络请求相关的方法 
   */
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        //保存res数据
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    //商品数据请求
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        //将请求到数据加入goods列表中
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;

        this.$refs.scroll.finishPullUp();
      });
    },
  },
};
</script>


<style scoped>
/*
scoped表示css的当前作用域 
 */
#home {
  /* margin-top: 44px; */
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: white;

  /* 在使用浏览器原生滚动时，起固定作用 */
  /* position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 2; */
}
.tab-control {
  /* 使用better-scroll插件时，已经实效 */
  /* position: sticky; */
  position: relative;
  z-index: 9;
}
.content {
  overflow: hidden;

  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
/* .fixed {
    position: fixed;
    left: 0;
    right: 0;
    top:44px;

  } */
</style>